package com.ruipengkj.view.memberView;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Date;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

import com.ruipengkj.bean.TCommodity;
import com.ruipengkj.bean.TExeStageTmpl;
import com.ruipengkj.bean.TRole;
import com.ruipengkj.bean.TSerShiftType;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.util.MatcherFormatUtil;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.JLabelJListComponent;
import com.ruipengkj.component.JLabelJRadioButtonComponent;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.component.RPJButtonBlue;
import com.ruipengkj.component.RPJButtonGray;
import com.ruipengkj.service.TExeStageTmplService;
import com.ruipengkj.service.TRoleService;
import com.ruipengkj.service.TSerShiftTypeService;
import com.ruipengkj.view.base.BaseDialog;

public class TExeStageTmplUpdate extends BaseDialog {
	private static final long serialVersionUID = 1L;
	private JButton okButton;//确定
	private JButton cancel;//取消
	private JLabelJTextFieldComponent name= null;//阶段名称
	private JLabelJListComponent roleID = null; //实施角色
	private JLabelJListComponent serID = null; //轮牌
	private JLabelJRadioButtonComponent isLocation= null;//是否需要实施场所
	private JLabelJTextFieldComponent commodityID = null;//商品编号
	private JLabelJRadioButtonComponent isSer = null;//是否需要轮牌
	private TExeStageTmpl tExeStageTmplEntity = new TExeStageTmpl();
	public boolean isSuccess=false;//判断是否添加成功
	private TExeStageTmplService tExeStageTmplService=BuildSpringApplication.getInstance().getTExeStageTmplServiceImpl();
	private TSerShiftTypeService tSerShiftTypeService=BuildSpringApplication.getInstance().getTSerShiftTypeServiceImpl();
	private TRoleService tRoleService =BuildSpringApplication.getInstance().getTRoleServiceImpl(); 
	private List<TSerShiftType> serList;
	private List<TRole> roleList;
	private boolean isNew=false;
	private String AllExeID;
	public TExeStageTmplUpdate(JDialog jd, String title, boolean b) {
		
		super(jd,title,b);
		this.init();
		this.setSize(300, 550);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
	}
	private void init(){
		this.initViews();
		this.initListener();
	}
	private void initListener(){
		okButton.addActionListener(this);
		cancel.addActionListener(this);
		isSer.addYesActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				roleID.setVisible(false);
				serID.setVisible(true);
				serID.setSelected(new TRole(AllExeID));
				isNew=false;
			}
			
		});
		isSer.addNoActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				roleID.setVisible(true);
				serID.setVisible(false);
				roleID.setSelected(new TRole(AllExeID));
				isNew=true;
			}
			
		});
		okButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		Date date=new Date();
		int sers=serID.getList().getSelectedIndex();
		int roles=roleID.getList().getSelectedIndex();
		TExeStageTmpl tExeStageTmpl= new TExeStageTmpl();
		tExeStageTmpl=tExeStageTmplEntity;
		String nameTF=name.getText().getText().trim(); 		
			if(e.getSource()==okButton){
				//确定
				if(!MatcherFormatUtil.MatcherPriceScheme(nameTF)){
					JOptionPane.showMessageDialog(null, "阶段名称不能为空");
					name.getText().setText("");
					name.getText().requestFocus();
					return;
				}
				if(!isNew && sers==-1){
					JOptionPane.showMessageDialog(null, "请选择轮牌顺序！");
					serID.getList().setSelectedIndex(0);
					return;
				}
				if(isNew && roles==-1){
					JOptionPane.showMessageDialog(null, "请选择实施人员！");
					roleID.getList().setSelectedIndex(0);
					return;
				}
				try {
					tExeStageTmpl.setName(nameTF);
					tExeStageTmpl.setNeedLocation(isLocation.getSelect());
					tExeStageTmpl.setNeedSerShift(isSer.getSelect());
					if(!isNew){
						tExeStageTmpl.setOperatorRole(serList.get(sers).getTypeID());
					}else if(isNew){
						tExeStageTmpl.setOperatorRole(roleList.get(roles).getRoleID());
					}
					TCommodity _tCommodity=new TCommodity();
					_tCommodity.setCommodityID(commodityID.getText().getText().trim());
					tExeStageTmpl.setTCommodity(_tCommodity);
					tExeStageTmplService.saveOrUpdateService(tExeStageTmpl);
					JOptionPane.showMessageDialog(null, "操作成功!");
					isSuccess=true;
					this.dispose();
				} catch (Exception e1) {
					e1.printStackTrace();
					isSuccess=false;
				}
				
			}else if(e.getSource() == cancel){
				this.dispose();
			}
		
	}
	private void initViews() {
		JPanel main_jp=new JPanel();
		BoxLayout boxLayout=new BoxLayout(main_jp,BoxLayout.Y_AXIS);
		main_jp.setLayout(boxLayout);
		main_jp.setBackground(Util.PANEL_COLOR);
		name = new JLabelJTextFieldComponent("阶段名称 ：", 30);
		isLocation=new JLabelJRadioButtonComponent("是否有实施场所：",60);
		commodityID=new JLabelJTextFieldComponent("商品编号：",30);
		isSer=new JLabelJRadioButtonComponent("是否需要轮牌：",60);
		try {
			serList = tSerShiftTypeService.findService(null);
			serID=new JLabelJListComponent("轮牌：",serList.toArray());
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			 roleList=tRoleService.findService(null);
			roleID=new JLabelJListComponent("实施人员角色：",roleList.toArray());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JPanel jp1=new JPanel();
		okButton=new RPJButtonBlue("添加");
		cancel = new RPJButtonGray("取消");
		jp1.add(okButton);
		jp1.add(cancel);
		jp1.setOpaque(false);
		
		main_jp.add(name);
		main_jp.add(roleID);
		main_jp.add(serID);
		main_jp.add(isLocation);
		main_jp.add(commodityID);
		main_jp.add(isSer);
		main_jp.add(jp1);
		
		this.add(main_jp);
	}
	public void showDialog(){
		this.setVisible(true);
	}
	public void showDialog(TExeStageTmpl tExeStageTmpl){
		if(tExeStageTmpl!=null){
			tExeStageTmplEntity=tExeStageTmpl;
			name.getText().setText(tExeStageTmpl.getName());
			
			commodityID.getText().setText(tExeStageTmpl.getTCommodity().getCommodityID());
			if(tExeStageTmpl.isNeedLocation()){
				isLocation.setSelect(1+"");
			}else{
				isLocation.setSelect(0+"");
			}
			if(tExeStageTmpl.isNeedSerShift()){
				AllExeID=tExeStageTmpl.getOperatorRole();
				serID.setSelected(new TSerShiftType(tExeStageTmpl.getOperatorRole()));
				serID.setVisible(true);
				roleID.setVisible(false);
				isSer.setSelect(1+"");
				isNew=false;
			}else{
				AllExeID=tExeStageTmpl.getOperatorRole();
				roleID.setSelected(new TRole(tExeStageTmpl.getOperatorRole()));
				isSer.setSelect(0+"");
				serID.setVisible(false);
				roleID.setVisible(true);
				isNew=true;
			}
		}
		this.setVisible(true);
	}
}
